package com.epam.training.service.penalty;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.epam.training.domain.Penalty;
import com.epam.training.repository.PenaltyRepository;

@Service
public class PenaltyServiceImpl implements PenaltyService {
	private static final int PAGE_SIZE = 10;
	@Autowired
	private PenaltyRepository repository;

	@Override
	public List<Penalty> getAllPenalty() {
		return repository.findAll();
	}

	@Override
	public Page<Penalty> getAllPenalty(Integer pageNumber) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, PAGE_SIZE);
		return repository.findAll(pageRequest);
	}

	@Transactional
	@Override
	public void save(Penalty penalty) {
		repository.save(penalty);
	}

	@Override
	public Penalty getById(Integer id) {
		return repository.findOne(id);
	}

	@Override
	public void delete(Integer id) {
		repository.delete(id);
	}

	@Override
	public void update(Penalty penalty, Integer id) {
		Penalty penaltyDB = repository.findOne(id);
		
		penaltyDB.setName(penalty.getName());
		penaltyDB.setAmount(penalty.getAmount());
		penaltyDB.setDescription(penalty.getDescription());
		
		repository.save(penalty);
	}

}
